package com.cardone.platform.authority.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.authority.dto.UsGroupOrgDto;

/**
 * 用户组与组织
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class UsGroupOrgJdbcDao implements UsGroupOrgDao {
	@Override
	public int deleteByIds(final UsGroupOrgDto deleteUsGroupOrg) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteUsGroupOrg.getAttrs())) {
			model.putAll(deleteUsGroupOrg.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteUsGroupOrg.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final UsGroupOrgDto findUsGroupOrg) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsGroupOrg, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findUsGroupOrg, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findUsGroupOrg.getAttrs())) {
			model.putAll(findUsGroupOrg.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final UsGroupOrgDto findUsGroupOrg) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsGroupOrg);

		final Model model = ModelUtils.newModel(new ModelArgs(findUsGroupOrg, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findUsGroupOrg.getAttrs())) {
			model.putAll(findUsGroupOrg.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final UsGroupOrgDto findListUsGroupOrg) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListUsGroupOrg).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListUsGroupOrg.getAttrs())) {
			model.putAll(findListUsGroupOrg.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final UsGroupOrgDto insertUsGroupOrg) {
		if (StringUtils.isBlank(insertUsGroupOrg.getId())) {
			insertUsGroupOrg.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertUsGroupOrg));

		if (MapUtils.isNotEmpty(insertUsGroupOrg.getAttrs())) {
			model.putAll(insertUsGroupOrg.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final UsGroupOrgDto paginationUsGroupOrg) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationUsGroupOrg).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationUsGroupOrg.getAttrs())) {
			model.putAll(paginationUsGroupOrg.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationUsGroupOrg), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final UsGroupOrgDto readUsGroupOrg) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readUsGroupOrg, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readUsGroupOrg.getAttrs())) {
			model.putAll(readUsGroupOrg.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readUsGroupOrg);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readUsGroupOrg, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final UsGroupOrgDto saveUsGroupOrg) {
		final P oldUsGroupOrg = this.findByCodeOrId(mappedClass, saveUsGroupOrg);

		if (oldUsGroupOrg == null) {
			this.insertByCode(saveUsGroupOrg);

			return this.findByCodeOrId(mappedClass, saveUsGroupOrg);
		}

		final int updateCount = this.updateByCode(saveUsGroupOrg, oldUsGroupOrg);

		if (updateCount < 1) {
			return oldUsGroupOrg;
		}

		return this.findByCodeOrId(mappedClass, saveUsGroupOrg);
	}

	@Override
	public int updateByCode(final UsGroupOrgDto updateUsGroupOrg) {
		final UsGroupOrgDto oldUsGroupOrg = this.findByCodeOrId(UsGroupOrgDto.class, updateUsGroupOrg);

		return this.updateByCode(updateUsGroupOrg, oldUsGroupOrg);
	}

	private <P> int updateByCode(final UsGroupOrgDto updateUsGroupOrg, final P oldUsGroupOrg) {
		if (oldUsGroupOrg == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateUsGroupOrg);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateUsGroupOrg, oldUsGroupOrg, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldUsGroupOrg, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldUsGroupOrg, updateUsGroupOrg, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateUsGroupOrg, updateProperties), new ModelArgs(updateUsGroupOrg, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateUsGroupOrg.getAttrs())) {
			model.putAll(updateUsGroupOrg.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}